<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-08 15:19:22
 * @Last Modified time: 2023-02-08 15:48:49
 */
namespace app\common\services\store;

use app\common\dao\store\ShopDao;

use frank\base\BaseService;
use frank\exception\CommonException;
use frank\utils\helper\ArrayHelper;
use app\common\services\RegionService;

/**
 * Class ShopService
 */
class ShopService extends BaseService
{

    /**
     * ShopService constructor.
     * @param ShopDao $dao
     */
    public function __construct(ShopDao $dao)
    {
        $this->dao = $dao;
    }

    public function getShopDetail($filter)
    {
        $detail = $this->dao->detail($filter);
        if (!$detail) {
            throw new CommonException('门店不存在');
        }
        return $detail->toArray();
    }

    /**
     * 获取列表
     */
    public function getShopList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getShopList($where, $page, $limit);
        return $list;
    }

    /**
     * 获取列表
     */
    public function getShopSelect(array $where = [])
    {
        $select = $this->dao->getShopSelect($where);
        return $select;
    }




    public function getAddressRegionData(array $params = []){
    	$regionService = RegionService::instance();
    	// dump($params);die;
    	$provinceId = $regionService->getIdByName($params['region'][0], 1);
        $cityId = $regionService->getIdByName($params['region'][1], 2, $provinceId);
        $regionId = $regionService->getIdByName($params['region'][2], 3, $cityId);
        // 验证城市ID是否合法
        if ($cityId <= 0){
            _trace('system_error','选择的城市不存在');
            throw new CommonException('很抱歉，您选择的城市不存在');
	    }
    	$data = [
        	'shop_name' => $params['shop_name'],
            'phone' => $params['phone'],
            'shop_logo' => $params['shop_logo'],
            'shop_image' => $params['shop_image'],
            'start_time' => $params['start_time'],
            'end_time' => $params['end_time'],
            'location'=>$params['location'],
            'province_id' => $provinceId,
            'city_id' => $cityId,
            'region_id' => $regionId,
            'address' => $params['address'],
            'is_check' => $params['is_check'],
            'district' => ($regionId === 0 && !empty($params['region'][2])) ? $params['region'][2] : '',
        ];
    	return $data;
	}
}

